<?php

namespace app\admin\controller;

use app\common\BaseHelper as Helper;

use app\admin\model\Image;

use think\Request;

class Notice extends Base
{

    public function __construct() {
        parent::__construct();

        $this->redirect = 'admin/notice/index';
        $this->searchFields = [
            'title' => [
                'label'     => '标题',
                'field'     => 'title',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
        ];
    }

    /**
     * 列表
     */
    public function index() {
        $request = $this->request;
        $param = $request->param();

        if ($request->param('reset')) {
            return redirect(fullUrl($request->path()));
        }

        if($request->isAjax()){
            $model = model($this->model);
            // 每页起始条数
            $start = $param['start'];
            // 每页显示条数
            $length = $param['length'];
            // 排序条件
            $columns = $param['order'][0]['column'];
            $order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

            $where = $this->getFilterWhere($request);

            if($this->defaultWhere){
                //$model = $model->where($this->defaultWhere);
                $where = array_merge($this->defaultWhere, $where);
            }
            /*if($this->defaultOrder){
                $model = $model->order($this->defaultOrder);
            }*/
            $list = $model->where($where)->limit($start, $length)->order($order)->select();
            $count = $model->where($where)->count();
            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count,
            ];

            return json($result);
        }

        return parent::index();
    }


    public function add(Request $request) {
        $model = model('department');
        $info = $model->where(['status'=>1])->select();
        $this->assign('info',$info);
        return $this->view->fetch('add', []);
    }

    public function addPost(Request $request, $redirect = '') {
        $model = model($this->model);

        //save data
        if ($request->isPost()) {
            $data = $request->param();
            // Insert data
            $data['id'] = Helper::getUUID();
            $data['createDate'] = date('Y-m-d H:i:s', time());

            $result = $model->create($data, true);

            if ($result) {
                return $this->success('添加成功！', $this->redirect);
            } else {
                return $this->error('添加失败！');
            }
        }
    }

    public function edit(Request $request) {



        $id = $request->param('id');
        $model = model($this->model);
        $where['id'] = $id;
        $info = $model->where($where)->find();
        $this->assign('info',$info);

        $model = model('department');
        $data = $model->where(['status'=>1])->select();
        $this->assign('data',$data);

        if(!$info){
            return $this->error('error !', 'admin/' . strtolower($this->model) . '/index');
        }

        return $this->view->fetch('edit', [
            'info' => $info
        ]);
    }

    public function editPost(Request $request, $redirect = '') {
        $model = model($this->model);

        //save data
        if ($request->isPost()) {
            $data = $request->param();
            // Update data
            $data['updateDate'] = date('Y-m-d H:i:s',time());
            $result = $model->allowField(true)->save($data, ['id' => $data['id']]);

            if($result !== false) {
                return $this->success('编辑成功！', $this->redirect);
            } else {
                return $this->error($model->getError());
            }
        } else {
            return $this->error('error !');
        }
    }

    public function detail(Request $request) {
        $model = model($this->model);
        $id = $request->param('id');
        // Get data info
        $info = $model->find($id);

        if(!$info){
            return $this->error('error !', url($this->redirect));
        }

        $info['publisher'] = $this->publisher;

        return $this->view->fetch('detail', [
            'info' => $info
        ]);
    }

    /**
     * 删除
     */
    public function delete(Request $request)
    {
        $id = $request->param('id');
        $model = model($this->model);
        $where = [
            'id' => $id
        ];
        $result = $model->where($where)->delete();
        if($result !== false) {
            return $this->success('删除成功', $this->redirect);
        } else {
            return $this->error('删除失败 !');
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['title']) && $filter['title']){
                $where['title'] = ['like', '%'.$filter['title'].'%'];
            }
        }

        return $where;
    }
}
